//package com.spider.im.chat.dao;
//
//import com.spider.im.chat.model.ChatGroup;
//import com.spider.im.chat.utils.DatabaseManager;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import java.sql.*;
//import java.time.LocalDateTime;
//import java.util.ArrayList;
//import java.util.List;
//
//public class ChatGroupDAO {
//    private static final Logger logger = LoggerFactory.getLogger(ChatGroupDAO.class);
//
//    // 插入一个新的ChatGroup
//    public void insertChatGroup(ChatGroup chatGroup) {
//        String sql = "INSERT INTO chat_group (group_name, group_description, group_owner, group_members, group_status, created_at, updated_at, group_tags_list) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
//        try (Connection connection = DatabaseManager.getConnection();
//             PreparedStatement statement = connection.prepareStatement(sql)) {
//            statement.setString(1, chatGroup.getGroupName());
//            statement.setString(2, chatGroup.getGroupDescription());
//            statement.setString(3, chatGroup.getGroupOwner());
//            statement.setString(4, chatGroup.getGroupMembers());
//            statement.setString(5, chatGroup.getGroupStatus());
//            statement.setTimestamp(6, Timestamp.valueOf(chatGroup.getCreatedAt()));
//            statement.setTimestamp(7, Timestamp.valueOf(chatGroup.getUpdatedAt()));
//            statement.setString(8, String.join(",", chatGroup.getGroupTagsList()));
//            statement.executeUpdate();
//        } catch (SQLException e) {
//            logger.error("Error adding chat group: {}", e.getMessage());
//        }
//    }
//
//    // 根据ID查询ChatGroup
//    public ChatGroup getChatGroupById(Long id) {
//        String sql = "SELECT * FROM chat_group WHERE id = ?";
//        try (Connection connection = DatabaseManager.getConnection();
//             PreparedStatement statement = connection.prepareStatement(sql)) {
//            statement.setLong(1, id);
//            try (ResultSet resultSet = statement.executeQuery()) {
//                if (resultSet.next()) {
//                    return mapResultSetToChatGroup(resultSet);
//                }
//            }
//        } catch (SQLException e) {
//            logger.error("Error getting chat group by id: {}", e.getMessage());
//        }
//        return null;
//    }
//
//    // 查询所有ChatGroup
//    public List<ChatGroup> getAllChatGroups() {
//        List<ChatGroup> chatGroups = new ArrayList<>();
//        String sql = "SELECT * FROM chat_group";
//        try (Connection connection = DatabaseManager.getConnection();
//             Statement statement = connection.createStatement();
//             ResultSet resultSet = statement.executeQuery(sql)) {
//            while (resultSet.next()) {
//                chatGroups.add(mapResultSetToChatGroup(resultSet));
//            }
//        } catch (SQLException e) {
//            logger.error("Error getting all chat groups: {}", e.getMessage());
//        }
//        return chatGroups;
//    }
//
//    // 更新ChatGroup
//    public void updateChatGroup(ChatGroup chatGroup) {
//        String sql = "UPDATE chat_group SET group_name = ?, group_description = ?, group_owner = ?, group_members = ?, group_status = ?, created_at = ?, updated_at = ?, group_tags_list = ? WHERE id = ?";
//        try (Connection connection = DatabaseManager.getConnection();
//             PreparedStatement statement = connection.prepareStatement(sql)) {
//            statement.setString(1, chatGroup.getGroupName());
//            statement.setString(2, chatGroup.getGroupDescription());
//            statement.setString(3, chatGroup.getGroupOwner());
//            statement.setString(4, chatGroup.getGroupMembers());
//            statement.setString(5, chatGroup.getGroupStatus());
//            statement.setTimestamp(6, Timestamp.valueOf(chatGroup.getCreatedAt()));
//            statement.setTimestamp(7, Timestamp.valueOf(chatGroup.getUpdatedAt()));
//            statement.setString(8, String.join(",", chatGroup.getGroupTagsList()));
//            statement.setLong(9, chatGroup.getId());
//            statement.executeUpdate();
//        } catch (SQLException e) {
//            logger.error("Error updating chat group: {}", e.getMessage());
//        }
//    }
//
//    // 删除ChatGroup
//    public void deleteChatGroup(Long id) {
//        String sql = "DELETE FROM chat_group WHERE id = ?";
//        try (Connection connection = DatabaseManager.getConnection();
//             PreparedStatement statement = connection.prepareStatement(sql)) {
//            statement.setLong(1, id);
//            statement.executeUpdate();
//        } catch (SQLException e) {
//            logger.error("Error deleting chat group: {}", e.getMessage());
//        }
//    }
//
//    // 将ResultSet映射为ChatGroup对象
//    private ChatGroup mapResultSetToChatGroup(ResultSet resultSet) throws SQLException {
//        ChatGroup chatGroup = new ChatGroup();
//        chatGroup.setId(resultSet.getLong("id"));
//        chatGroup.setGroupName(resultSet.getString("group_name"));
//        chatGroup.setGroupDescription(resultSet.getString("group_description"));
//        chatGroup.setGroupOwner(resultSet.getString("group_owner"));
//        chatGroup.setGroupMembers(resultSet.getString("group_members"));
//        chatGroup.setGroupStatus(resultSet.getString("group_status"));
//        chatGroup.setCreatedAt(resultSet.getTimestamp("created_at").toLocalDateTime());
//        chatGroup.setUpdatedAt(resultSet.getTimestamp("updated_at").toLocalDateTime());
//        chatGroup.setGroupTagsList(List.of(resultSet.getString("group_tags_list").split(",")));
//        return chatGroup;
//    }
//
//    public static void main(String[] args) {
//        ChatGroupDAO chatGroupDAO = new ChatGroupDAO();
//
//        try {
//            // 插入ChatGroup
//            ChatGroup chatGroup = new ChatGroup();
//            chatGroup.setGroupName("Test Group");
//            chatGroup.setGroupDescription("This is a test group");
//            chatGroup.setGroupOwner("admin");
//            chatGroup.setGroupMembers("user1,user2");
//            chatGroup.setGroupStatus("active");
//            chatGroup.setCreatedAt(LocalDateTime.now());
//            chatGroup.setUpdatedAt(LocalDateTime.now());
//            chatGroup.setGroupTagsList(List.of("tag1", "tag2"));
//            chatGroupDAO.insertChatGroup(chatGroup);
//
//            // 查询ChatGroup
//            ChatGroup fetchedChatGroup = chatGroupDAO.getChatGroupById(3L);
//            System.out.println(fetchedChatGroup.getGroupName());
//
//            // 更新ChatGroup
//            fetchedChatGroup.setGroupDescription("Updated description");
//            chatGroupDAO.updateChatGroup(fetchedChatGroup);
//
//            // 删除ChatGroup
////            chatGroupDAO.deleteChatGroup(1L);
//        } catch (Exception e) {
//            logger.error("Error in main method: {}", e.getMessage());
//        }
//    }
//}
